/**
 * @author yadongzhao87
 * @date 2023-10-19
 * 清雷毫米波雷达相关接口
 */
const router = require('koa-router')();
const returnData = require("../util/returnstring");
const dotenv = require("dotenv");
const {
    InfluxDB,
    Point
} = require('@influxdata/influxdb-client');

// const fileLogger = require('pino')( pino.destination('/log/path'))
dotenv.config({ path: `.env.${process.env.NODE_ENV}` })

const url = process.env.INFLUXDB_URL;
const token = process.env.INFLUXDB_TOKEN;
const org = process.env.INFLUXDB_ORG;
const client = new InfluxDB({
    url,
    token
})
// 定义路由前缀，没有可不添加
router.prefix('/api/hujiao')
// 获取清雷的实时报警回调
router.post('/pushmedicallstatemsg', async (ctx, next) => {
    console.log(ctx.request.body);
    let callbackData = ctx.request.body;
    
    const currentDate = new Date();
    const year = currentDate.getFullYear();
    const month = currentDate.getMonth() + 1; // getMonth()返回的月份从0开始，所以需要加1
    const day = currentDate.getDate();
    const hours = currentDate.getHours();
    const minutes = currentDate.getMinutes();
    const seconds = currentDate.getSeconds();
    const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    // const inserData = [
    //     {
    //         caller_deviceNumber: callbackData.callerDeviceNumber,
    //         callUUID: callbackData.callUUID,
    //         call_type: callbackData.callType,
    //         call_groupType: callbackData.callGroupType,
    //         call_groupNumber: callbackData.callGroupNumber,
    //         call_state: callbackData.callState,
    //         call_time: callbackData.callTime,
    //         heart_time: callbackData.heartTime,
    //         call_timestamp: callbackData.timestamp,
    //         created_at: formattedDate
    //     }

    // ]
    const caller_deviceNumber=callbackData.callerDeviceNumber;
    const callUUID=callbackData.callUUID;
    const call_type=callbackData.callType;
    const call_groupType= callbackData.callGroupType;
    const call_groupNumber=callbackData.callGroupNumber;
    const call_state=callbackData.callState;
    const call_time=callbackData.callTime;
    const heart_time=callbackData.heartTime;
    const call_timestamp=callbackData.timestamp;
    try{
        let org = `benyi`
        let bucket = `iot`
        let writeClient = client.getWriteApi(org, bucket, 'ns')
        let point = new Point('hujiao')
            .tag('isOnline', true).tag("snCode", caller_deviceNumber).intField('alarmTime', call_timestamp).stringField('alarmType', call_state).intField('callUUID', callUUID);
        writeClient.writePoint(point)
        writeClient.flush();
        console.log('\nFinished SUCCESS')
    }catch(e){
        console.log('\nFinished ERROR')
    }
       

    ctx.body=returnData(100000,[],"数据写入成功");
});
module.exports = router;